package org.lc.service.system.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.lc.service.system.entity.SysRoleAclApi;
import org.lc.service.system.entity.SysUser;

import java.util.List;

@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {


    @Select(value = """
            SELECT 
                ROLE.code code,
            	CONCAT(
            		CASE
             			sm.moda
             			WHEN "LINK" THEN
             			sm.link
             			WHEN "IFRAME" THEN
             			sm.frame ELSE sm.router
             		END
            	,":" ,sp.code) acl ,
            	sm.api api,
            	sp.id permission
            FROM
            	(
            	SELECT
            		sr.code code,
            		sr.ID ID
            	FROM
            		( SELECT ru.sys_role_id AS role_id FROM sys_role_user ru WHERE ru.sys_user_id = #{id}) sru
            		LEFT JOIN sys_role sr ON sru.role_id = sr.id
            	)
            	ROLE LEFT JOIN sys_role_permission srp ON srp.sys_role_id = ROLE.
            	ID LEFT JOIN sys_permission sp ON srp.sys_permission_id = sp.
            	ID LEFT JOIN sys_menu sm ON sm.ID = sp.menu_id
            """)
    List<SysRoleAclApi> findRoleByUserId(@Param("id") String id);


    @Select("""
            SELECT
            	sr.code code,
            	CONCAT(
            	    CASE
             			sm.moda
             			WHEN "LINK" THEN
             			sm.link
             			WHEN "IFRAME" THEN
             			sm.frame ELSE sm.router
             		END
            	,":" ,sp.code) acl,
            	sm.api api,
            	sp.id permission
            FROM
            	( SELECT du.sys_department_id AS department_id FROM sys_department_user du WHERE du.sys_user_id =  #{id} ) dept
            	LEFT JOIN sys_department_role sdr ON sdr.sys_department_id = dept.department_id
            	LEFT JOIN sys_role sr ON sr.ID = sdr.sys_role_id
            	LEFT JOIN sys_role_permission srp ON srp.sys_role_id = sr.
            	ID LEFT JOIN sys_permission sp ON sp.id = srp.sys_permission_id
            	LEFT JOIN sys_menu sm ON sm.ID = sp.menu_id
                        """)
    List<SysRoleAclApi> findDepartmentRoleByUserId(@Param("id") String id);
}